
package com.wyzz.hr.bo.impl;

import com.wyzz.hr.base.Constants;
import com.wyzz.hr.bo.IBaseBo;
import com.wyzz.hr.dao.IEmployeeDAO;
import com.wyzz.hr.util.GenericValidator;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;


public class BaseBoImpl implements IBaseBo, Constants {

	public BaseBoImpl() {
	}

	public IEmployeeDAO getEmployeeDAO() {
		return employeeDAO;
	}

	public void setEmployeeDAO(IEmployeeDAO employeeDAO) {
		this.employeeDAO = employeeDAO;
	}

	public String getNoByPrefix(String domainName, String colName, String pre) {
		List result = employeeDAO.exeHqlList((new StringBuilder()).append(
				"select max(").append(colName).append(") from ").append(
				domainName).append(" where ").append(colName).append(" like '")
				.append(pre).append("%' ").toString());
		if (result.size() > 0) {
			String no = (String) result.get(0);
			return getNewNo(no, pre);
		}
		if (GenericValidator.isNumber(pre))
			return pre;
		else
			return (new StringBuilder()).append(pre.toUpperCase()).append(
					"0001").toString();
	}

	private String getNewNo(String no, String pre) {
		DecimalFormat decimalFormat;
		String maxNo;
		StringBuffer fix;
		try {
			if (GenericValidator.isNumber(no)) {
				DecimalFormat allNumbers = new DecimalFormat("###########");
				return allNumbers.format((new BigDecimal(no))
						.add(new BigDecimal(1)));
			}
		} catch (Exception e) {
			return (new StringBuilder()).append(pre.toUpperCase()).append(
					"0001").toString();
		}
		decimalFormat = new DecimalFormat("0000");
		maxNo = no.replaceAll((new StringBuilder()).append("(?i)").append(pre)
				.toString(), "");
		if (GenericValidator.isNumber(maxNo))
			return no.replaceAll(maxNo, decimalFormat.format(Double.valueOf(
					maxNo).doubleValue() + 1.0D));
		fix = new StringBuffer();
		StringBuffer num = new StringBuffer();
		for (int len = 0; len < no.length(); len++) {
			String str = no.substring(len, len + 1);
			if (GenericValidator.isNumber(str))
				num.append(str);
			else
				fix.append(str);
		}

		if (num.length() > 0)
			return fix.append(
					decimalFormat.format(Integer.valueOf(num.toString())
							.intValue() + 1)).toString();
		return (new StringBuilder()).append(fix.toString().toUpperCase())
				.append("0001").toString();
	}

	private IEmployeeDAO employeeDAO;
}
